package com.hsck.order.kds.base.utils;

import android.text.TextUtils;
import android.util.Log;

import com.hsck.order.kds.BuildConfig;


/**
 * 创建者 : ly
 * 时间 : 2017/9/13
 * 公司 : 工博
 * 功能 : 日志工具类
 */

public class LogUtil {

    private static boolean isDebug = BuildConfig.DEBUG;

    private static String getTag() {
        StackTraceElement element = (new Throwable().fillInStackTrace().getStackTrace())[2];

        StringBuffer sb = new StringBuffer("==")
                .append(element.getClassName()).append(".")
                .append(element.getMethodName()).append("():")
                .append(element.getLineNumber());
        return sb.toString().replace("com.example.orderingsystem.","");
    }

    public static void v(String mess) {
        if (isDebug) {
            v(getTag(), mess);
        }
    }

    public static void v(String tag, String mess) {
        if (isDebug) {
            if (!TextUtils.isEmpty(mess)) {
                log("v",tag, mess);
            }
        }
    }

    public static void d(String mess) {
        if (isDebug) {
            d(getTag(), mess);
        }
    }

    public static void d(String tag, String mess) {
        if (isDebug) {
            if (!TextUtils.isEmpty(mess)) {
                log("d",tag, mess);
            }
        }
    }

    public static void i(String mess) {
        if (isDebug) {
            i(getTag(), mess);
        }
    }

    public static void i(String tag, String mess) {
        if (isDebug) {
            if (!TextUtils.isEmpty(mess)) {
                log("i",tag, mess);
            }
        }
    }

    public static void w(String mess) {
        if (isDebug) {
            w(getTag(), mess);
        }
    }

    public static void w(String tag, String mess) {
        if (isDebug) {
            if (!TextUtils.isEmpty(mess)) {
                log("w",tag, mess);
            }
        }
    }

    public static void e(String mess) {
        if (isDebug) {
            e(getTag(), mess);
        }
    }

    public static void e(String tag, String mess) {
        if (isDebug) {
            if (!TextUtils.isEmpty(mess)) {
                log("e",tag, mess);
            }
        }
    }

    private static void log(String level,String tag, String msg){
        if (!TextUtils.isEmpty(msg)) {
            //因为String的length是字符数量不是字节数量所以为了防止中文字符过多，
            //  把4*1024的MAX字节打印长度改为2001字符数
            int max_str_length = 2001 - tag.length();
            //大于4000时
            while (msg.length() > max_str_length) {
                logLevel(level,tag, msg.substring(0, max_str_length));
                msg = msg.substring(max_str_length);
            }
            logLevel(level,tag, msg);
        }
    }

    private static void logLevel(String level,String tag, String msg){
        switch (level){
            case "v":
                Log.v(tag,msg);
                break;
            case "d":
                Log.d(tag,msg);
                break;
            case "i":
                Log.i(tag,msg);
                break;
            case "w":
                Log.w(tag,msg);
                break;
            case "e":
                Log.e(tag,msg);
                break;
        }
    }
}
